Coloring and Matching
Bipartite Graphs
The chromatic number of a graph , denoted by , is the smallest integer for which the vertices of can be colored by colors so that adjacent vertices are colored by different colors.
A 2-colorable graph is called bipartite. Equivalently, is bipartite if the vertex set of can be split into the disjoint sets and (the color classes) so that each edge of is adjacent to one vertex of and one vertex of .
Theorem 1
A graph is bipartite if and only if it does not contain a cycle of an odd length.
Proof
Let us assume that contains the odd cycle . Let us assume without loss of generality that is red. Then must be blue, therefore must be red, and so on, and at the end, must be red, too.
This is not allowed, however, as is an edge. To prove the "if" part, let be a graph with no odd cycles. Let be a vertex of , and color red. Define the color of any other vertex as follows. If the shortest path from to has even length, then let be red, and if the shortest path from to has odd length, then let be blue. We show that this is a good coloring, that is, there are no two adjacent vertices that are the same color.
Let us assume the contrary, by first assuming that and are two red vertices that are joined by an edge. Let the shortest path from to be , and let the shortest path from to be . Then and both have an even number of edges, so walking from through to , then through , then back from through to , we get a closed walk with an odd number of edges. Taking away edges that were used both by and , this walk splits into the union of edge-disjoint cycles. As the total number of edges in these cycles is still odd, there has to be at least one cycle with an odd number of edges, which is a contradiction.
If we assumed instead that both and were blue, the same proof would work as the sum of two odd numbers is still even, so would still have an odd number of edges.
Theorem 2
Let be a simple bipartite graph on vertices. Then has at most edges if is even, and at most edges, if is odd
Proof
Choose so that no other simple bipartite graph on vertices has more edges than . Denote by and the sizes of the two color classes of . It is clear that each vertex of one color class is connected to each vertex of the other color class in . Indeed, if there was a missing edge between the two color classes, we could add it to , contradicting to our assumption. So has edges, and the proof follows from elementary calculus. (One simply has to find the integer for which the number is maximal.)
Lemma
Let be a simple graph on vertices and at least edges. Then contains a triangle.
Proof
We prove our statement by induction on . If , then is a subgraph of with at least five edges. Theorem 11.6 shows that is not bipartite, so it must have an odd cycle. This odd cycle must be a triangle as has only four vertices.
Now assume we know that the statement is true for all integers that are smaller than , and are at least 2. Let be as in the statement of the Theorem, and let and be two adjacent vertices in . If the sum of the degrees of and is more than , then they have a common neighbor , and so is a triangle. If, on the other hand, the sum of the degrees of and is at most , then deleting , and all the edges adjacent to them from will decrease the number of edges in our graph by at most . (Note that the edge is contained twice in the sum of the two degrees.) Therefore, after the deletion of these vertices and edges, we are left with a graph of vertices, and at least edges. Such a graph contains a triangle by the induction hypothesis, so our claim is proved.
Theorem 3
Let be a simple graph on vertices and at least edges. Then contains at least triangles.
Proof
If , then we are done as we found our missing triangles.
If , then the total number of edges between and the outside vertices is at most . As itself contains three edges, it follows that there are at least edges within the subgraph spanned by all outside vertices. If we omit the vertex of which has the smallest degree in , it follows by the Pigeon-hole Principle that we get a graph on vertices that still has more than
edges. So has strictly more than edges, that is, it has at least of them. Therefore, by the induction hypothesis, there are at least triangles within . As we said in the previous paragraph, there are triangles spanned by two vertices of and an outside vertex. In our case, , so we have again found the needed triangles.
Finally, consider the case when the number of edges connecting outside vertices to is not more than . Note that we can assume that there is at least one such edge, otherwise has edges, so adding any vertex of to creates a graph on vertices and vertices, and the proof follows by the induction hypothesis. That said, the number of edges within is at least . Adding a vertex of that is adjacent to at least one outside vertex to creates a graph with vertices and at least edges, and again, the induction hypothesis shows that such a graph must contain at least triangles.
Matchings
Let be any graph, and let be a set of edges in so that no two edges in have a vertex in common. Then we say that is a matching in . If each vertex in is covered by an edge in , then we call a perfect matching.
Philip Hall's Theorem
Let be a bipartite graph. Then has a perfect matching into if and only if for all , the inequality holds.
Proof
We prove the statement by induction on , the initial case being trivial. Now assume we know the statement for all nonnegative integers less than , and prove it for . Let us assume that for all , the inequality holds. We distinguish two cases.
First let us assume that for each proper subset , even the strict inequality holds. Let and be adjacent vertices, with . Let , and let be any nonempty subset of . Our assumption then shows that , therefore . Consequently, the induction hypothesis implies that can be matched into in . Adding the edge to this matching, we get a perfect matching of into .
Now assume there is a subset so that holds. We split into two smaller subgraphs and , and then show that each of these subgraphs satisfies the induction hypothesis separately. Let be the subgraph induced by , and let be the graph obtained from by deleting all vertices that belong to .
To see that satisfies the induction hypothesis, choose any subset . Then , and therefore, , (all neighbors of are within , and therefore, .
To see that satisfies the induction hypothesis, choose any subset . Then , and because this is a union of disjoint sets, .
If we apply the induction hypothesis to both and , we see that can be matched into (and therefore, onto), , and can be matched into . Therefore, can be matched into as claimed.
In a graph , a matching is called maximal if we cannot extend by adding a new edge to it. A matching is called maximum if no matchings of contain more edges than .
Let be a graph, and let be a matching in . A path is called an -alternating path if is in if and only if is not in .
Theorem 2
Let be any simple graph, and let be a matching in . Then is maximum if and only if has no -augmenting paths.
Proof
To prove the "if" part, assume there is no -augmenting path in , and let be any maximum matching in . Consider , the set of edges that are part of exactly one of and . As and are both matchings, the connected components of can only be even cycles or alternating paths. However, is maximum, and there is no augmenting path, therefore all these alternating paths are of even length. This implies , and our claim is proved.
Stable Matchings
Let be a bipartite graph with a perfect matching , with each vertex of given an ordered list of preferences for the vertices of , and each vertex of given an ordered list of preferences for the vertices of . We say that is stable if the following two conditions do not both hold.
- There is a vertex so that , but prefers to , and
- if and are the vertices defined in part (a), and , then prefers over
Gale-Shapley Algorithm
The algorithm starts by having each element in one set (traditionally the men) propose to their most preferred element in the other set (traditionally the women). Then, each element in the other set (traditionally the women) reviews their proposals and tentatively accepts the proposal from their most preferred proposer.
If a woman receives a proposal from a man who is not her most preferred, she will reject the proposal and continue to consider other proposals. If a man's proposal is rejected, he will move on to his next most preferred choice and propose again. This process continues until all stable matches have been made.
A matching is considered stable if there are no two pairs where one person in each pair prefers the other person to their current partner. The Gale-Shapley algorithm guarantees that a stable matching will always be found, regardless of the preferences of the individuals involved.
Code Implementation
def gale_shapley(men, women):
# Initialize all men and women to be free
free_men = men.keys()
free_women = set(women.keys())
# Initialize an empty dictionary to store the matches
matches = {}
# While there are free men, continue proposing
while free_men:
# Choose a free man
man = free_men.pop()
# Get the man's preference list
pref_list = men[man]
# Propose to each woman on the list
for woman in pref_list:
if woman in free_women:
# The woman is free, so they become a match
matches[man] = woman
free_women.remove(woman)
break
else:
# The woman is already matched, so check if she prefers this man over her current partner
current_man = matches[woman]
woman_pref_list = women[woman]
if woman_pref_list.index(man) < woman_pref_list.index(current_man):
# The woman prefers this man, so they become a match and the previous match is freed
matches[man] = woman
free_men.add(current_man)
matches[current_man] = None
break
return matches